<?php

declare(strict_types=1);

namespace App\Goods\Model;

use Mine\MineModel;

/**
 * @property int $id 主键ID
 * @property int $goods_id 商品ID
 * @property int $category_id 商品分类ID
 * @property int $store_id 商城ID
 * @property int $create_time 创建时间
 * @property int $update_time 更新时间
 */
class GoodsCategoryRel extends MineModel
{
    public bool $timestamps = false;
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'goods_category_rel';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'goods_id', 'category_id', 'store_id', 'create_time', 'update_time'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'goods_id' => 'integer', 'category_id' => 'integer', 'store_id' => 'integer', 'create_time' => 'integer', 'update_time' => 'integer'];

    public static function increased(int $goodsId, array $categoryIds, int $storeId = null)
    {
        $dataset = [];
        foreach ($categoryIds as $categoryId) {
            $dataset[] = [
                'category_id' => $categoryId,
                'goods_id' => $goodsId,
                'store_id' => !empty($storeId) ? $storeId : 0,
                'create_time'=>time(),
                'update_time'=>time()
            ];
        }
        return self::query()->insert($dataset);
    }
}
